package org.nod.atgm.gameobjects;

import org.nod.atgm.model.CollisionManager;

import com.threed.jpct.CollisionEvent;
import com.threed.jpct.CollisionListener;
import com.threed.jpct.Logger;
import com.threed.jpct.Object3D;
import com.threed.jpct.Primitives;
import com.threed.jpct.SimpleVector;

public class ArtilleryShell extends MovingObject {
	
	public static final String ID = "ArtilleryShell";

	static SimpleVector initialPosition = new SimpleVector(0f,-2f,-50f);
	float gravityCoef = 0.00002f;
	SimpleVector dirVector = new SimpleVector(0,0,0);

	protected boolean destroyed = false;
	
	@Override
	public void handleTimer(int time) {
		if (destroyed)
			 return;
		float multiplicator = scalarSpeed * time;
		
		destroyed = CollisionManager.checkProjectileCollision(this,speedVector,multiplicator);
		if (!destroyed) {
			float x = speedVector.x * multiplicator;
			float y = speedVector.y * multiplicator;
			float z = speedVector.z * multiplicator;
			translate(x,y,z);
			speedVector.y +=  9.8f * time * gravityCoef;
	//		dirVector.set(speedVector); //TODO abandoned it for now. Orientation is calc'ed incorrectly & due to some reason object gets squashed(!)
	//		dirVector.y = 0;
	//		gameObject.setOrientation(speedVector, dirVector);
		}
	}

//	@Override
//	protected Object3D createObject() {
//		Object3D precompiledObject = PrecompiledObjectManager.getInstance().getPrecompiledObject(ID);
//		Object3D object = new Object3D(precompiledObject, true);
//		object.shareCompiledData(precompiledObject);
//		object.compile();
//	
////		object.setTexture("texture");
////		object.rotateX((float) (Math.PI / 2));
//	
////		object.addCollisionListener(new CollisionListener() {
////			
////			/**
////			 * 
////			 */
////			private static final long serialVersionUID = -5975677479314827373L;
////
////			@Override
////			public boolean requiresPolygonIDs() {
////				return false;
////			}
////			
////			@Override
////			public void collision(CollisionEvent event) {
////				fireCollisionEvent(event);
////			}
////		});
//		return object;
//	}

	public float getGravityCoef() {
		return gravityCoef;
	}

	public void setGravityCoef(float gravityCoef) {
		this.gravityCoef = gravityCoef;
	}
	
	public static Object3D createBasicArtilleryShellObject() {
		Object3D object = Primitives.getSphere(10,0.5f);
		object.calcTextureWrap();
		object.setTexture("texture");//XXX
		object.compile();
		return object;
	}

	@Override
	protected String getId() {
		return ID;
	}

	@Override
	protected void setupObject(Object3D object) {
		object.translate(initialPosition);
		object.setCollisionMode(Object3D.COLLISION_CHECK_SELF);
	}

}
